# -*- coding: UTF-8 –*-
__author__ = 'yangq'
import json

from tornado.httpclient import AsyncHTTPClient, HTTPError
from tornado import gen

import settings
from appcommon.appinstance import mysql_app_pool


@gen.coroutine
def authenticate(user_name, password):
    """
    Check the user's validity from the login page through a particular API.
    Implement asynchronous for fetch action with API connection.

    :param username: utf-8
    :param password: utf-8
    :return: {"code":0, "DeptID":"613","DepartmentStr":"无线互联研发部"}
    """
    sql = "select 1 from user_info where user_name = '{}' and password = '{}' and is_using = 1"
    user = yield mysql_app_pool.get_one(sql.format(user_name, password))
    res = 1 if user else 0
    raise gen.Return(res)
